package com.neusoft.neuiotms.dcam.fixed.converter.spareconsume;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeBackDTO;
import com.neusoft.neuiotms.dcam.fixed.model.SpareType;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeBack;
import com.neusoft.neuiotms.dcam.fixed.repository.SpareTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StorehouseRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeBackRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetCodeRuleService;

@Component
public class SpareConsumeBackConver extends AbstractConverter<SpareConsumeBack, SpareConsumeBackDTO> {

    @Autowired
    private SpareConsumeBackRepository spareConsumeBackRepository;

    @Autowired
    private AssetCodeRuleService assetCodeRuleService;

    @Autowired
    private SpareTypeRepository spareTypeRepository;

    @Autowired
    private StorehouseRepository storehouseRepository;

    @Override
    public SpareConsumeBack toModel(final SpareConsumeBackDTO dto) {
        SpareConsumeBack model;
        if (dto.isNew()) {
            model = new SpareConsumeBack();
        } else {
            model = this.spareConsumeBackRepository.getOne(dto.getId());
        }
        model.setSn(dto.getSn());
        model.setSpecification(dto.getSpecification());
        String spareCode = StringUtils.EMPTY;
        if (dto.getSpareTypeId() != null) {
            final SpareType spareType = this.spareTypeRepository.findByCode(dto.getSpareTypeId());
            model.setSpareType(spareType);
            spareCode = spareType.getCode();
        }
        if (StringUtils.isNotEmpty(dto.getStorckNumber())) {
            model.setStorckNumber(dto.getStorckNumber());
        } else {
            model.setStorckNumber(
                    this.assetCodeRuleService.generateAssetCode(spareCode, dto.getStorehouseId().toString(),
                            StringUtils.EMPTY));
        }
        if (dto.getStorehouseId() != null) {
            model.setStorehouse(this.storehouseRepository.getOne(dto.getStorehouseId()));
        }
        model.setWorkNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        return model;
    }

    @Override
    public SpareConsumeBackDTO toDTO(final SpareConsumeBack model, final boolean forListView) {
        final SpareConsumeBackDTO dto = new SpareConsumeBackDTO();
        dto.setId(model.getId());
        dto.setSn(model.getSn());
        if (model.getSpareType() != null) {
            dto.setSpareTypeId(model.getSpareType().getCode());
            dto.setSpareTypeName(model.getSpareType().getName());
        }
        dto.setSpecification(model.getSpecification());
        dto.setStorckNumber(model.getStorckNumber());
        if (model.getStorehouse() != null) {
            dto.setStorehouseId(model.getStorehouse().getId());
            dto.setStorehouseName(model.getStorehouse().getName());
        }
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkNum());
        return dto;
    }
}
